@include('admin::layouts.header')
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>图片列表</legend>
    </fieldset>
	<div class="demoTable layui-row">
		<div class="layui-form layui-form-pane" style="float:left;width:460px">
			<div class="layui-form-item">
				<label class="layui-form-label">添加时间</label>
				<div style="width:72%" class="layui-input-inline layui-form">
					<input style="width:100%" type="text" class="layui-input" id="created_at" placeholder="请选择时间">
				</div>
			</div>	
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">状态</label>
				<div class="layui-input-inline layui-form">
					<select id="status_d">
						<option value="">请选择</option>
						@foreach($status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">类型</label>
				<div class="layui-input-inline layui-form">
					<select id="open">
						<option value="">请选择</option>
						@foreach($image_type as $k=>$v)
							<option value="{{$k}}">{{$v}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" style="margin-left:30px;margin-top:15px;" id="search" data-type="reload">查询</button>
		<a href="{{route('admin.image.index')}}" style="margin-top:15px;" class="layui-btn">重置</a>
	</div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
@include('admin::layouts.footer')
<script type="text/html" id="status">
	<input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="{!! config('admin.enabled') !!}|{!! config('admin.forbidden') !!}" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}> 
</script>
<script type="text/html" id="image">
	<a class="example-image-link" href="@{{d.url}}" data-lightbox="example-set" data-title="">
	<img src="@{{d.url}}" style="width:100px;height:100%;"/></a>
</script>
<script type="text/html" id="topBtn">
   <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" id="delAll">清理无用图片</button>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
			id: 'ad',
            elem: '#list',
            url: "{{route('admin.image.data')}}",
            method:'get',
			toolbar: '#topBtn',
			title:'图片列表',
			page: true,
            cols: [[
                {field:'id', title: '编号', width:100,align: 'center',fixed: true,sort: true}
                ,{field:'open', title: '类型',align: 'center', width:200}
                ,{field:'url', title: '图片',align: 'center', width:200, toolbar: '#image'}
				,{field:'status', title: '状态',width:150,align: 'center',toolbar: '#status'}
                ,{field:'created_at', title: '添加时间',align: 'center', width:200,sort: true}
                ,{field:'updated_at', title: '删除时间',align: 'center', width:200,sort: true}
            ]],
			limit: "{!! config('admin.common_limit') !!}"
        });
		//搜索
        $('#search').on('click', function () {
			var created_at 		= $('#created_at').val();
			var status  		= $('#status_d').val();
			var open  			= $('#open').val();
            if ($.trim(created_at) === '' && $.trim(status) === '' && $.trim(open) === ''){
                layer.msg("{!! config('admin.query_condition') !!}", {icon: 0});
                return;
            }
			tableIn.reload({
				where: {created_at:created_at,status:status,open:open}
			});
        });
        form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('/admin/image/'+id+'/status',{_method:'PUT',status:status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		$('#delAll').click(function(){
            layer.confirm('确定要清除无用图片吗？', {icon: 3}, function(index) {
                layer.close(index);
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{{ route('admin.image.destroy_all') }}", {_method:'DELETE'}, function (res) { 
                    layer.close(loading);
					console.log(res);
                    if (res.status == 1) {
                        layer.msg(res.msg,{time:1000,icon:1},function(){
							location.href = res.url;
						});
                    } else {
                        layer.msg(res.error, {time: 1000, icon: 2});
                    }
                });
            })
        });
    });
</script>
</body>
</html>